/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.service.merchant;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.service.BaseService;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.modules.ivm.dao.merchant.MerchantClauseDao;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;
import com.neusoft.lohas.modules.ivm.entity.merchant.MerchantClause;

/**
 * 服务条款Service
 * @author Jackie.Liu
 * @version 2014-10-30
 */
@Component
@Transactional(readOnly = true)
public class MerchantClauseService extends BaseService {

	@Autowired
	private MerchantClauseDao merchantClauseDao;
	
	public MerchantClause get(String id) {
		return merchantClauseDao.get(id);
	}
	
	public Page<MerchantClause> find(Page<MerchantClause> page, MerchantClause merchantClause) {
		DetachedCriteria dc = merchantClauseDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(merchantClause.getContent())){
			dc.add(Restrictions.like("name", "%"+merchantClause.getContent()+"%"));
		}
		dc.add(Restrictions.eq(MerchantClause.FIELD_DEL_FLAG, MerchantClause.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return merchantClauseDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(MerchantClause merchantClause) {
		merchantClauseDao.save(merchantClause);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		merchantClauseDao.deleteById(id);
	}
	
	public MerchantClause findByMerchantId(String merchantId){
		MerchantClause merchantClause = merchantClauseDao.findByMerchantId(merchantId);
		if (merchantClause == null) {
			merchantClause = new MerchantClause();
			merchantClause.setMerchant(new Merchant(merchantId));
		}
		return merchantClause;
	}
	
}
